﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Model;
using SportsStore.Repository.Interfaces;
using SportsStore.ServicesInterfaces;

namespace SportsStore.Services
{
  public class CartItemService : ICartItemService
  {
    private readonly ICartItemRepository _cartItemRepository;

    public CartItemService(ICartItemRepository cartItemRepository)
    {
      _cartItemRepository = cartItemRepository;
    }
 
    public void AddItem(int productId, string shoppingCartId, int quantity)
    {
      CartItem cartItem;
      try
      {
        cartItem = _cartItemRepository.GetCart(productId, shoppingCartId);
      }
      catch (Exception e)
      {
        throw new SportsStoreException("Unable to get cart item", e);
      }

      if (cartItem == null)
      {
        cartItem = new CartItem(productId, shoppingCartId, quantity);

        try
        {
          _cartItemRepository.Create(cartItem);
        }
        catch (Exception e)
        {
          throw new SportsStoreException("Unable to create cart item", e);
        }
      }
      else
      {
        cartItem.Quantity += quantity;

        try
        {
          _cartItemRepository.Update(cartItem);
        }
        catch (Exception e)
        {
          throw new  SportsStoreException("Unable to update cart item", e);
        }
      }
    }

    public int RemoveItem(int productId, string shoppingCartId)
    {
      CartItem cartItem;
      try
      {
        cartItem = _cartItemRepository.GetCart(productId, shoppingCartId);
      }
      catch (Exception e)
      {
        throw new SportsStoreException("Unable to get cart item", e);
      }

      int itemCount = 0;

      if (cartItem != null)
      {
        if (cartItem.Quantity > 1)
        {
          cartItem.Quantity--;
          itemCount = cartItem.Quantity;

          try
          {
            _cartItemRepository.Update(cartItem);
          }
          catch (Exception e)
          {
            throw new SportsStoreException("Unable to update cart item", e);
          }
        }
        else
        {
          try
          {
            _cartItemRepository.Delete(cartItem);
          }
          catch (Exception e)
          {
            throw new SportsStoreException("Unable to delete cart item", e);
          }
        }
      }
      return itemCount;
    }

    public decimal ComputeTotalValue(string shoppingCartId)
    {
      try
      {
        return _cartItemRepository.GetTotal(shoppingCartId);
      }
      catch (Exception e)
      {
        throw new SportsStoreException("Unable to get total cart value", e);
      }
    }

    public void ClearBasket(string shoppingCartId)
    {
      IEnumerable<CartItem> cartItems;
      try
      {
        cartItems = _cartItemRepository.GetCart(shoppingCartId);
      }
      catch (Exception e)
      {
        throw new SportsStoreException("Unable to get cart item", e);
      }

      foreach (var cartItem in cartItems)
      {
        try
        {
          _cartItemRepository.Delete(cartItem);
        }
        catch (Exception e)
        {
          throw new SportsStoreException("Unable to get delete cart item", e);
        }
      }
    }

    public IEnumerable<CartItem> GetCartItems(string cartId)
    {
      try
      {
        return _cartItemRepository.GetCart(cartId);
      }
      catch (Exception e)
      {
        throw new SportsStoreException("Unable to get cart items");
      }
    }
  }
}
